/**
 * 
 */
package com.ginger.security.shiro;

import java.util.HashSet;
import java.util.Set;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * @Description: ShiroRealm
 * @author 姜锋
 * @date 2018年5月24日 上午9:37:02 
 * @version V1.0   
 *
 */
public class CustomRealm extends AuthorizingRealm {
	
	/**
	 * 授权
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		
		//String username = (String) principals.getPrimaryPrincipal();
		Set<String> roles = new HashSet<>();
		roles.add("admin");
		Set<String> permissions = new HashSet<>();
		permissions.add("user:delete");
		SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
		authorizationInfo.setStringPermissions(permissions);
		authorizationInfo.setRoles(roles);
		return authorizationInfo;
	}
	
	/**
	 * 认证
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		
		//String username = (String)token.getPrincipal();
		
		//String password = "123456";
		
		SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo("ginger","123456","customRealm");
		return authenticationInfo;
	}
}
